<?php

namespace App\Transformers;

use App\Models\Order;
use App\Models\Shop;

class ShopTransformer extends BaseTransformer
{
    protected $availableIncludes = ['keeper_info', 'keeper_detail', 'products'];

    public function transform(Shop $model)
    {
        $data = [
            'id' => $model->id,
            'name' => $model->name,                                         #店名
            'user_id' => $model->user_id,                                   #用户id
            'face_picture' => image_url($model->face_picture),              #店铺图片
            'begin_business_hours' => $model->begin_business_hours,         #开始营业时间
            'end_business_hours' => $model->end_business_hours,             #结束营业时间
            'begin_free_time' => $model->begin_free_time,                   #结束营业时间
            'end_free_time' => $model->end_free_time,                       #结束营业时间
            'province' => $model->province,                                 #省
            'city' => $model->city,                                         #市
            'district' => $model->district,                                 #区
            'status' => $model->status,                                     #店铺状态
            'sort' => $model->sort,
            'deal_total_amount' => getMoneyFormat($model->product_orders
                ->where('status', Order::STATUS_FINISH)
                ->where('refund_status', Order::REFUND_STATUS_PENDING)
                ->whereBetween('created_at', [date('Y-m-01 00:00:00'), date('Y-m-t 23:59:59')])
                ->sum('total_amount')),
        ];

        return $this->_select($data);
    }

    public function includeKeeperInfo(Shop $model)
    {
        return $this->item($model->user, new UserTransformer());
    }

    public function includeKeeperDetail(Shop $model)
    {
        return $this->item($model->data, new UserDataTransformer());
    }

    public function includeProducts(Shop $model)
    {
        return $this->collection($model->products->where('is_show', true), new ProductTransformer());
    }
}
